<?php
/*
 * 建立登陆和首页模块
 * @datta 2016/7/7
 * @author 孙玉峰
 */
class IndexAction extends Action {
    public function login(){
		if($this->_post('sub')){
			$name=$this->_post('name');
			$pwd=md5($this->_post('pwd'));
			$user  =M('user');
			$sql = "select * from `w_user` where `u_name`='".$name."' and `u_pwd` = '".$pwd."'";
			$query = mysql_query($sql);
			$data = mysql_fetch_assoc($query); //mysql_fetch_assoc() 函数从结果集中取得一行作为关联数组
			if($data){
				session('u_name',$data['u_name']);
				session('u_nickname',$data['u_nickname']);
				session('u_nickname');
				$this->success('登陆成功','__URL__/shouye');
			}
			else{
				$this->error('登陆失败','__URL__/login');
			}
		}
		else{
			$this->display();
		}
    }
    public function shouye(){
		$this->assign('name',session('u_name'));
		$this->assign('nickname',session('u_nickname'));
    	$this->display();
    }
   

     /*
     * 建立注册的方法
     * @param none
     * @return none
     */
    public function zhuce(){

    	$this->display();   //指向注册页面
    }
    public function register(){
    	
    	$username = trim($_POST['username']);  //trim 去除开始于结尾的空格
    	$pwd   =  md5($_POST['pwd']);     //md5加密  密码转换为一长串的字符
    	$nickname=$_POST['nickname'];
    	$user  =M('user');
    	//双引号要与最近的一个双引号相匹配
    	$sql = "select u_name from `w_user` where u_name = '".$username."'";
    	$query = mysql_query($sql);
    	$num = mysql_num_rows($query);
    	if(empty($pwd) || empty($username)){
    		$this->error('注册失败，密码或名字不能为空');
    	}
    	if(!empty($num)){
    		$this->error('此用户名已存在');
    	}else{
    		$data['u_name'] = $username;
    		$data['u_pwd'] = $pwd;
    		$data['u_nickname']=$nickname;
    		$reslut = $user->add($data);
    		if($reslut){
    			$this->success('注册成功','__URL__/login');
    		}else{
    			$this->error('注册失败','__URL__/register');
    		}
    	}
    }
    /*
     * 建立退出的方法
     * @param none
     * @return none
     */
    public function ex(){
    	session('u_name',null);
    	session('u_nickname',null);
    	$this->success('退出成功','__URL__/login');
    }
   
}
?>
<meta http-equiv="content-type" content="text/html; charset=utf-8">